<?php

class User_UserInfo {
    const USER_INFO = "user_v2_userinfo";
    const GROUP = "default";

    public static function user_info($uid=-1){
        $query = DB::select()->from(self::USER_INFO);
        if($uid != -1){
            $query = $query->where('uid','=',$uid);
        }
        $query = $query->execute(self::GROUP);
        return $query;
    }

    public static function create($data){
        list($insert_id, $rows) = DB::insert(self::USER_INFO)
            ->columns (array_keys($data))
            ->values (array_values($data))
            ->execute(self::GROUP);
        return $insert_id;
    }

    public static function update($data,$key,$value){
        $rows = DB::update(self::USER_INFO)
            ->set($data)
            ->where($key, '=', $value)
            ->execute(self::GROUP);
        return $rows;
    }

    public static function user_checkout($key,$val){
        $query = DB::select()
            ->from(self::USER_INFO)
            ->where($key,'=',$val)
            ->execute(self::GROUP);
        return $query;
    }


    public static function add_resource($file,$path){
        $fileType = strstr($file['type'],'image');

        if(!$fileType){
            $msg =  "不是图片文件";
            return array($msg,false);
        }

        if ($file["error"] > 0){
            $msg =  "Return Code: " . $file["error"] . "<br />";
            return array($msg,false);
        }
        else{
            $ext = explode('.',$file['name']);
            $files = $path.md5($file['name'].time()).".".$ext[count($ext)-1];
            move_uploaded_file($file["tmp_name"],$files);

//            $img = Image::factory($files);
//            $img->resize(250, 250);
//            $img->save();
            $width   = 200;
            $height  = 200;

            Cropper::crop($files, $width, $height);
            return array("/".$files,true);
        }
    }

    public static function add_resource_pic($file,$path){
        $fileType = strstr($file['type'],'image');

        if(!$fileType){
            $msg =  "不是图片文件";
            return array($msg,false);
        }

        if ($file["error"] > 0){
            $msg =  "Return Code: " . $file["error"] . "<br />";
            return array($msg,false);
        }
        else{
            $ext = explode('.',$file['name']);
            $files = $path.md5($file['name']).".".$ext[count($ext)-1];
            move_uploaded_file($file["tmp_name"],$files);

//            $img = Image::factory($files);
//            $img->resize(250, 250);
//            $img->save();
//            $width   = 200;
//            $height  = 200;
//
//            Cropper::crop($files, $width, $height);
            return array("/".$files,true);
        }
    }


}
